<?php
class LoginController extends Zend_Controller_Action
{ 
	public function init()
    {
        /* Initialize action controller here */
    	defined('BASE_URL')	|| define('BASE_URL', Zend_Controller_Front::getInstance()->getBaseUrl());
    }
    public function indexAction()
    {
    	
    	$this->_helper->layout()->disableLayout();///sopharat disablelayout to display login
    	
  		$tr = Application_Form_FrmLanguages::getCurrentlanguage();
		if($this->getRequest()->isPost()){
			$formdata=$this->getRequest()->getPost();
			$db_user = new Application_Model_DbTable_DbUsers();
			$email=$formdata['txt_email'];
			$password=$formdata['txt_password'];
			if($db_user->checkEmail($email)){
				if($db_user->userAuthenticate($email,$password)){
					$user_id=$db_user->getUserID($email);
					$user_info = $db_user->getUserInfo($user_id);
					//print_r($user_info['user_type_id']);exit();
					$arr_acl = $db_user->getArrAcl($user_info['user_type_id']);
					//print_r($arr_acl);exit();
					//in case user have no right to access any module of the system
					if(!$arr_acl) $this->view->msg = $tr->translate('LOGIN_FAIL_NO_MODULE');
					else{
						$session_user=new Zend_Session_Namespace('auth');
						$session_user->unlock();
						$session_user->user_id=$user_id;
						$session_user->fullname=$user_info['fullname'];
						$session_user->user_name=$user_info['username'];
						$session_user->level=$user_info['user_type_id'];
						$session_user->user_type=$user_info['user_type'];
						$session_user->email=$email;
						for($i=0; $i<count($arr_acl);$i++){
							$arr_module[$i]=$arr_acl[$i]['module'];
						}
						$arr_module = array_unique($arr_module);
												$session_user->arr_acl = $arr_acl;
						$session_user->arr_module = $arr_module;												
						$session_user->lock();
						//echo $session_user->user_name;
						//echo $session_user->user_id=$user_id;exit();
						//$_url=($arr_acl[0]!=='')? '/'.$arr_acl[0]['module']:'/default/index/home' ;//before
						//print_r($user_info['user_type_id']);exit();
						if($user_info['user_type_id']== 1){
							$_url=($arr_acl[0]!=='')? '/default/index/dashboad':'/default/index/home';
						}
						elseif($user_info['user_type_id']==2){
							$_url=($arr_acl[0]!=='')? '/default/index/dashboad':'/default/index/home' ;//after
						}
						elseif($user_info['user_type_id']==3){
							$_url=($arr_acl[0]!=='')? '/default/index/dashboad':'/default/index/home' ;//after
						}
						elseif($user_info['user_type_id']==4){
							$_url=($arr_acl[0]!=='')? '/default/index/dashboad':'/default/index/home' ;//after
						}
						elseif($user_info['user_type_id']==5){
							$_url=($arr_acl[0]!=='')? '/default/index/dashboad':'/default/index/home' ;//after
						}
						elseif($user_info['user_type_id']==6){
							$_url=($arr_acl[0]!=='')? '/default/index/dashboad':'/default/index/home' ;//after
						}
						elseif($user_info['user_type_id']==7){
							$_url=($arr_acl[0]!=='')? '/default/index/dashboad':'/default/index/home' ;//after
						}
						elseif($user_info['user_type_id']== 8){
							$_url=($arr_acl[0]!=='')? '/foundation/index/index':'/foundation/index/index';
						}
						
						$this->_redirect($_url);
					}
				}
				elseif (!$db_user->checkStatusByEmail($email)){
					$this->view->msg = $tr->translate('LOGIN_FAIL_COMFIRM');
				}
				else{
					$this->view->msg = $tr->translate('LOGIN_FAIL');
				}
			} else{
				$this->view->msg  = $tr->translate('EMAIL_NOT');
			}
		}
	}		 
 	public function logoutAction()
    {
    	if($this->getRequest()->getParam('value')==1){
    		$aut=Zend_Auth::getInstance();
    		$aut->clearIdentity();
    		$session_user=new Zend_Session_Namespace('auth');
    		$session_user->unsetAll();
    		$session_stock=new Zend_Session_Namespace('stock');
    		$session_stock->unsetAll();
    		$this->_redirect('/');
    		exit();
    	}
    }
    public function webElementsAction(){	
    }
    public function homeAction(){
    }
    public function dashboadAction(){
    	$formFilter = new Product_Form_FrmProductFilter();
    	$this->view->formFilter = $formFilter;
    	$list = new Application_Form_Frmlist();
    	
    	$user_info = new Application_Model_DbTable_DbGetUserInfo();
    	$result = $user_info->getUserInfo();
    	 
    	$db = new Application_Model_DbTable_DbGlobal();
    	$productSql = "SELECT p.pro_id,p.item_name,p.p_code, lo.Name, pl.qty
    	FROM tb_product AS p
    	INNER JOIN tb_prolocation AS pl ON pl.pro_id = p.pro_id
    	INNER JOIN tb_sublocation AS lo ON lo.LocationId = pl.LocationId WHERE pl.qty < 10 ";
    	 
    	$str_condition = " AND pl.LocationId" ;
    	$productSql .= $db->getAccessPermission($result["level"], $str_condition, $result["location_id"]);
    	 
    	$productSql.= " ORDER BY p.pro_id";
    	
    	$rows=$db->getGlobalDb($productSql);
    	$link=array(
    			'module'=>'product','controller'=>'index','action'=>'update',
    	);
    	$columns=array("ITEM_NAME","ITEM_CODE","LOCATION_NAME","QTY_SM");
    	$urlEdit = BASE_URL . "/distributor/index/update";
    	
    	$urlEdit = BASE_URL . "/product/index/update";
    	$this->view->list=$list->getCheckList(1, $columns, $rows, array('item_name'=>$link), $urlEdit);
    	Application_Model_Decorator::removeAllDecorator($formFilter);
    }
   public function headToolbarAction(){
   	echo "this is testing";
   	
   } 
   
}